package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.atsmart.app.BizApp;
import com.atsmart.app.model.TemplateInfo;
import com.atsmart.app.model.TemplateListInfo;
import com.atsmart.app.utils.StringUtil;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
/**
 * TODO 模板表
 */
public class TemplateInfoSql {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	/* <string name="templatelist">Create TABLE if not exists templatelist( [_id] integer PRIMARY KEY
			    ASC AUTOINCREMENT, [templatelists] text(2000),[temID] integer(5));</string>*/
	public synchronized void addTemplateInfo(TemplateInfo at, int temId) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into templatelist (templatelists,temID) values(?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			String s = BizApp.getGson().toJson(at);
			stm.bindString(1, StringUtil.isEmptyOrNull(s) ? "" : s);
			stm.bindLong(2, temId);
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}
	
	public TemplateInfo getTemplateInfo(int tem_id) {
		String sql = " select _id,templatelists,temID from templatelist where temID=" + tem_id;
		Cursor cursor = db.rawQuery(sql, null);
		TemplateInfo historie = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TemplateInfo();
					historie.setId(cursor.getInt(1));
					historie.setName(cursor.getString(2));
					historie.setDesc(cursor.getString(3));
				}
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return  historie;
	}

	public TemplateListInfo getTemplateInfo(int begin, int pagesize) {
		String sql = "";
		sql = " select _id,templatelists,temID from templatelist" + " limit " + begin + "," + pagesize;
		 
		Cursor cursor = db.rawQuery(sql, null);
		TemplateInfo historie = null;
		List<TemplateInfo> nums = null;
		TemplateListInfo t = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<TemplateInfo>();
				t = new TemplateListInfo();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new TemplateInfo();
					String string = cursor.getString(1);
//					Log.e("TAG", "string = " + string);
					historie = BizApp.getGson().fromJson(string, TemplateInfo.class);
					nums.add(historie);
					cursor.moveToNext();
				}
				t.setTemplates(nums);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return t;
	}
	 
	public int getCount() {
		String sql = " select _id,templatelists,temID from templatelist";
		Cursor cursor = db.rawQuery(sql, null);
		try {
			if (cursor != null && cursor.moveToFirst()) {
				return cursor.getCount();
			}
		} catch (Exception e) {
			return 0;
		} finally {
			cursor.close();
		}
		return 0;
	}
	 
	public void deleteAttribute(int temID) {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from templatelist where temID="+ temID);
	}
	 
	public void delete() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from templatelist");
	}
}
